package com.ocean.quartz.task;

import com.ocean.productDict.mapper.oNcfileMonitorMapper;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ocean.common.utils.StringUtils;
import org.springframework.stereotype.Controller;

import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 定时任务调度测试
 * 
 * @author ocean
 */
@Component("ryTask")
public class RyTask
{

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    /**
     * 定时智能网格数据，归档到智能网格归档表
     */
    public static void selectZnwgData() throws IOException {
        String server = "114.116.246.158"; // FTP服务器地址
        int port = 21; // FTP服务器端口
        /*String user = "znwg_user"; // FTP登录用户名
        String pass = "qwer1234!@#$"; // FTP登录密码*/
        String user = "user_sjs"; // FTP登录用户名
        String pass = "Sjs#5723"; // FTP登录密码

        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            boolean login = ftpClient.login(user, pass);

            if (!login) {
                System.out.println("Login failed");
                ftpClient.disconnect();
                return;
            }
            // 切换到指定目录（可选）
            boolean b = ftpClient.changeWorkingDirectory("/MarineFCST/NMF/china_seas/whole/NMF_BEN_DT_CS_grid");
            ftpClient.enterLocalPassiveMode();//被动模式

            // 获取当前日期的开始和结束时间
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date startOfDay = cal.getTime();
            cal.add(Calendar.DAY_OF_YEAR, 1);
            Date endOfDay = cal.getTime();

            // 列出目录中的文件
            FTPFile[] files = ftpClient.listFiles();
            int length = files.length;
            for (FTPFile file : files) {
                if (file.isFile()) {
                    Date fileTimestamp = file.getTimestamp().getTime();
                    if (fileTimestamp != null && !fileTimestamp.before(startOfDay) && !fileTimestamp.after(endOfDay)) {
                        // 文件是在今天上传的
                        System.out.println(file.getName());
                    }
                }
               /* if (file.isFile()) {
                    System.out.println("File: " + file.getName());
                    System.out.println("Size: " + file.getSize() + " bytes");
                    // 你可以使用 file.getTimestamp().getTimeInMillis() 获取文件的修改时间等
                } else if (file.isDirectory()) {
                    System.out.println("Directory: " + file.getName());
                }*/
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        selectZnwgData();
    }
}
